package com.example.mywanandroid.ui.bottom.system.systemchildren;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.responsebean.WeChatBean;
import com.example.mywanandroid.config.Constants;
import com.example.mywanandroid.databinding.FragmentViewPagerBinding;
import com.example.mywanandroid.navinterface.ScrollToTop;
import com.example.mywanandroid.ui.adapter.FmPagerAdapter;
import com.example.mywanandroid.ui.articlelist.ArticleListFragment;

import java.util.ArrayList;
import java.util.List;

public class SystemChildrenFragment extends BaseFragment<FragmentViewPagerBinding, SystemChildrenViewModel> implements ScrollToTop {

    private List<WeChatBean> mList;
    private int pos;
    private FmPagerAdapter pagerAdapter;

    @Override
    protected void handleArguments(Bundle args) {
        pos = args.getInt(Constants.ParamCode.PARAM2);
        mList = (List<WeChatBean>) args.getSerializable(Constants.ParamCode.PARAM1);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_view_pager;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(SystemChildrenViewModel.class);
    }

    @Override
    protected void bindViewModel() {

    }

    @Override
    protected void init() {

        List<Fragment> fragments = new ArrayList<>();
        List<String> sTitle = new ArrayList<>();

        for (WeChatBean system : mList) {
            mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab().setText(system.getName()));
            sTitle.add(system.getName());
            fragments.add(ArticleListFragment.newInstance(1,system.getId()));
        }

        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.viewPager);

        pagerAdapter = new FmPagerAdapter(getChildFragmentManager(), fragments, sTitle);
        mDataBinding.viewPager.setAdapter(pagerAdapter);

        mDataBinding.viewPager.setCurrentItem(pos);
    }

    @Override
    public void scrollToTop() {
        ArticleListFragment fragment = (ArticleListFragment) pagerAdapter.getCurrentFragment();
        if (fragment != null) {
            fragment.scrollToTop();
        }
    }
}
